In this article we’ll discuss how you can setup a small server load monitoring bash script with a scheduled cron job to monitor your server’s load average, and e-mail you a notice if it’s above the limit that you’ve set.
A lot of times your server will probably be running at a load much lower than it can actually handle. Although at certain times your server’s usage might suddenly spike leading to the server possibly going unstable and it could potentially need a reboot to recover from.
Our system administration team has monitors setup on all of our dedicated servers for extreme load issues. However you might be interested in setting up some monitoring of your own so that you can be alerted via e-mail as soon as your server is spiking so that you can login to investigate that usage spike more closely.
This would primarily be used for a dedicated server. While it could also be used on a VPS the load average of a VPS typically shouldn’t exceed a 1.00 and it can temporarily fluctuate up and down as it’s a virtualized environment. So you might end up getting too many notices and by the time you get in to investigate it could have dropped back down.
Create bash load monitoring script
- First you’ll want to figure out how many CPU cores you have on your dedicated server as this determines the optimal load your server can operate at.
Â
You can login to your server via SSH and then run the following command to find this out:
grep pro /proc/cpuinfo -c
You should get back the number of cores on your server such as  4 .
- You won’t want your load average spiking much over how many CPU cores your server has, in this case I know this particular server isn’t doing anything other than serving up one static website, so if the load gets even up to a 4.00 I’ll want to investigate it. So I’m going to set my trigger value for load at a 4.00.
Â
Start editing a new file to create your bash alert script, in this case I’m using the vim text-editor and making a new file called loadMon in my user’s home directory with the following command:
vim /home/userna1/loadMon
Then you’ll want to hit i to enter Insert mode once vim is loaded up and enter in the following code:
#!/bin/bash trigger=4.00 load=`cat /proc/loadavg | awk '{print $1}'` response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'` if [[ $response = "greater" ]] then sar -q | mail -s"High load on server - [ $load ]" [email protected] fi
Here is the same code again with comments walking through what each line is doing:
#!/bin/bash
We set a trigger for how high the load can get before we’re
alerted via e-mail from this script.
trigger=4.00We set a load variable to read the current server load from
/proc/loadavg and only from the first column which is the live load.
load=`cat /proc/loadavg | awk ‘{print $1}’`We set a response variable to the word “greater” if the current
load is greater than our trigger that we set.
response=`echo | awk -v T=$trigger -v L=$load ‘BEGIN{if ( L > T){ print “greater”}}’`If the response is set to “greater” we run the sar -q command
and pipe | that data to the mail command for [email protected]
this sends an e-mail with the server’s recent load averages there.
if [[ $response = “greater” ]] then
sar -q | mail -s”High load on server – [ $load ]” [email protected]
fi
Setup cron job to run monitoring script
- Now that you have your load monitoring bash script setup, the next thing you’ll want to do is create a cron job to run this task, you can read about how to run a cron job.
Â
Typically setting up a cron job for monitoring of this type would be fine at every 5 minutes, you can select that premade option in cPanel’s cron jobs section, it should end up looking like the following once you’re done:
*/5 * * * * bash /home/userna1/loadMon
You should now have successfully setup a small bash script to monitor your servers load average, and a cron job to run that monitoring script on a set interval to send you an e-mail if the server’s load has exceeded the limit you’ve set.
Also,
you might want to mention that
“`
*/5 * * * * bash /home/userna1/loadMon
“`
has to be placed in crontab
ie.
“`
crontab -e
“`
hit ‘i’
then the command you want to cron
then save and exit
“` :wq “`
took a while to figure out
so here’s the tip 😛
Thanks for the tip! I hope that helps others in the community.
Hi, you might not do custom bash, but I am having the same error on centos6. So perhaps your code is not written for every version of linux.
# ./cpucheck.sh
./cpucheck.sh: line 13: syntax error near unexpected token `(‘
./cpucheck.sh: line 13: `response=`echo | awk -v T=$trigger -v L=$load ‘BEGIN{if ( L > T){ print “greater”}}’`’
How can we have the above output in webpage in terms of graphs ????????
We are happy to help, but it is not clear what you are trying to accomplish. Could you provide additional details or information?
Thank you,
John-Paul
I get an error
/myMonitor/loadMonitor: line 13: syntax error near unexpected token `(‘
/myMonitor/loadMonitor: line 13: `response=`echo | awk -v T=$trigger -v L=$load ‘BEGIN{if ( L > T){ print “greater”}}’`’
Hello,
This error states the file you are trying to load does not exist or you do not have permissions to view it.
Best Regards,
TJ Edens
Hello,
Unfortunately we do not assist with custom code solutions. I would suggest some tutorials on Bash programming or get with someone locally that knows the scripting language.
Best Regards,
TJ Edens
Hello,
Also I just used the code above and had no issues running it as the root user on a VPS.
Best Regards,
TJ Edens
try to remove the space around (
Hi there, i make the bash file but I received a mail”
“bash: /home/root/myMonitor/loadMonitor: No such file or directory”
Thank you for the useful script!
This is working fine. You can alter according to your requirements.
Nothing. Good article.
Awsome.. helpful